# Makefile for uClibc
#
# Copyright (C) 2000-2008 Erik Andersen <andersen@uclibc.org>
#
# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
#

subdirs += libc/stdlib

include $(top_srcdir)libc/stdlib/malloc/Makefile.in
include $(top_srcdir)libc/stdlib/malloc-simple/Makefile.in
include $(top_srcdir)libc/stdlib/malloc-standard/Makefile.in

CSRC-y := \
	abort.c getenv.c mkdtemp.c realpath.c canonicalize.c mkstemp.c mkostemp.c \
	rand.c random.c random_r.c setenv.c div.c ldiv.c lldiv.c \
	getpt.c drand48-iter.c jrand48.c \
	jrand48_r.c lcong48.c lrand48.c lrand48_r.c mrand48.c mrand48_r.c nrand48.c \
	nrand48_r.c rand_r.c srand48.c srand48_r.c seed48.c seed48_r.c \
	a64l.c l64a.c __uc_malloc.c
CSRC-$(UCLIBC_SUSV2_LEGACY) += valloc.c
CSRC-$(UCLIBC_HAS_ADVANCED_REALTIME) += posix_memalign.c
CSRC-$(UCLIBC_HAS_PTY) += grantpt.c unlockpt.c ptsname.c
CSRC-$(UCLIBC_HAS_ARC4RANDOM) += arc4random.c
CSRC-$(UCLIBC_HAS_LFS) += mkstemp64.c mkostemp64.c
CSRC-$(UCLIBC_HAS_FLOATS) += drand48.c drand48_r.c erand48.c erand48_r.c
CSRC-$(if $(findstring yy,$(UCLIBC_HAS_FLOATS)$(UCLIBC_SUSV3_LEGACY)),y) += \
	gcvt.c
CSRC-$(UCLIBC_SUSV3_LEGACY) += mktemp.c

ifneq ($(UCLIBC_HAS_BACKTRACE),)
CFLAGS-abort.c = -fasynchronous-unwind-tables
endif

# multi source stdlib.c
CSRC-y += abs.c labs.c atoi.c atol.c strtol.c strtoul.c _stdlib_strto_l.c \
	qsort.c qsort_r.c bsearch.c rpmatch.c \
	llabs.c atoll.c strtoll.c strtoull.c _stdlib_strto_ll.c
# (aliases) strtoq.o strtouq.o
CSRC-$(UCLIBC_HAS_FLOATS) += atof.c
CSRC-$(UCLIBC_HAS_XLOCALE) += strtol_l.c strtoul_l.c _stdlib_strto_l_l.c \
	strtoll_l.c strtoull_l.c _stdlib_strto_ll_l.c

CSRC-$(UCLIBC_HAS_WCHAR) += mblen.c mbtowc.c wctomb.c mbstowcs.c wcstombs.c \
	_stdlib_mb_cur_max.c _stdlib_wcsto_l.c _stdlib_wcsto_ll.c \
	wcstol.c wcstoul.c wcstoll.c wcstoull.c
CSRC-$(if $(findstring yy,$(UCLIBC_HAS_WCHAR)$(UCLIBC_HAS_XLOCALE)),y) += \
	_stdlib_wcsto_l_l.c _stdlib_wcsto_ll_l.c \
	wcstol_l.c wcstoul_l.c wcstoll_l.c wcstoull_l.c

# multi source _strtod.c
CSRC-$(UCLIBC_HAS_FLOATS) += strtod.c strtof.c strtold.c __strtofpmax.c __fp_range_check.c
CSRC-$(if $(findstring yy,$(UCLIBC_HAS_FLOATS)$(UCLIBC_HAS_XLOCALE)),y) += \
	strtod_l.c strtof_l.c strtold_l.c __strtofpmax_l.c
CSRC-$(if $(findstring yy,$(UCLIBC_HAS_FLOATS)$(UCLIBC_HAS_WCHAR)),y) += \
	wcstod.c wcstof.c wcstold.c __wcstofpmax.c
CSRC-$(if $(findstring yyy,$(UCLIBC_HAS_FLOATS)$(UCLIBC_HAS_WCHAR)$(UCLIBC_HAS_XLOCALE)),y) += \
	wcstod_l.c wcstof_l.c wcstold_l.c __wcstofpmax_l.c
# (aliases) wcstoq.o wcstouq.o
# wcstod wcstof wcstold

# multi source _atexit.c
CSRC-y += __cxa_atexit.c __cxa_finalize.c __exit_handler.c exit.c on_exit.c

STDLIB_DIR := $(top_srcdir)libc/stdlib
STDLIB_OUT := $(top_builddir)libc/stdlib

STDLIB_SRC := $(patsubst %.c,$(STDLIB_DIR)/%.c,$(CSRC-y))
STDLIB_OBJ := $(patsubst %.c,$(STDLIB_OUT)/%.o,$(CSRC-y))

libc-y += $(STDLIB_OBJ)
libc-static-y += $(STDLIB_OUT)/atexit.o $(STDLIB_OUT)/system.o
libc-static-$(COMPAT_ATEXIT) += $(STDLIB_OUT)/old_atexit.o
libc-shared-y += $(STDLIB_OUT)/system.oS

# this should always be the PIC version, because it could be used in shared libs
libc-nonshared-y += $(STDLIB_OUT)/atexit.os
libc-nonshared-$(COMPAT_ATEXIT) += $(STDLIB_OUT)/old_atexit.os
libc-nomulti-y += $(STDLIB_OUT)/labs.o $(STDLIB_OUT)/atol.o $(STDLIB_OUT)/_stdlib_strto_l.o $(STDLIB_OUT)/_stdlib_strto_ll.o
libc-nomulti-$(UCLIBC_HAS_XLOCALE) += $(STDLIB_OUT)/_stdlib_strto_l_l.o $(STDLIB_OUT)/_stdlib_strto_ll_l.o

objclean-y += CLEAN_libc/stdlib

CLEAN_libc/stdlib:
	$(do_rm) $(addprefix $(STDLIB_OUT)/*., o os oS)
